//
//  QZTabViewController.m
//  QZ浪里个浪
//
//  Created by 秦－政 on 2016/10/24.
//  Copyright © 2016年 pete. All rights reserved.
//

#import "QZTabViewController.h"
#import "QZTabBar.h"

@interface QZTabViewController ()

@end

@implementation QZTabViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 通过KVC添加自定义标签栏
    QZTabBar *tab = [[QZTabBar alloc] init];
    [self setValue:tab forKey:@"tabBar"];
    
    // 创建四个子控制器并设置它的相应内容
    UIViewController *vc1 = [self loadChildViewControllerWithClassName:@"QZHomeController" andTitle:@"首页" andImageName:@"tabbar_home"];
    UIViewController *vc2 = [self loadChildViewControllerWithClassName:@"QZMessageController" andTitle:@"消息" andImageName:@"tabbar_message_center"];
    UIViewController *vc3 = [self loadChildViewControllerWithClassName:@"QZDiscoverController" andTitle:@"发现" andImageName:@"tabbar_discover"];
    UIViewController *vc4 = [self loadChildViewControllerWithClassName:@"QZProfileController" andTitle:@"我" andImageName:@"tabbar_profile"];
    // 给标签控制器添加子控制器
    self.viewControllers = @[vc1, vc2, vc3, vc4];
    // 设置标签栏的主题颜色"它会影响标签栏上的所有文字颜色"
    self.tabBar.tintColor = [UIColor orangeColor];
}

- (UIViewController *)loadChildViewControllerWithClassName:(NSString *)className andTitle:(NSString *)title andImageName:(NSString *)imageName {
    // 把类名的字符串转成类的类型
    Class class =  NSClassFromString(className);
    // 通过转换出来的类的类型来创建控制器
    UIViewController *vc = [[class alloc] init];    
    // 设置标签栏上和导航栏对应控制器的标题
    vc.title = title;
    // 设置默认状态的图片
    vc.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 拼接选中状态的图片
    NSString *selectedImageName = [imageName stringByAppendingString:@"_selected"];
    // 设置选中图片
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 创建导航控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    // 返回导航控制器"导航控制器中又添加了一个根控制器"
    return nav;
}
@end
